package com.starinside.wp.controller.api;

import com.starinside.wp.domain.Feeds;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.domain.comment.Comment;
import com.starinside.wp.domain.comment.ExtCommentsSupport;
import com.starinside.wp.foundation.constant.FeedConstant;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import com.starinside.wp.foundation.vo.FeedsVO;
import com.starinside.wp.service.BindedBabyService;
import com.starinside.wp.service.CommentsService;
import com.starinside.wp.service.FeedsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @Describle This Class Is
 * @Author ZengMin
 * @Date 2018/11/7 10:08
 * @Company Matt
 */
@RequestMapping("/wx/api/comments")
@RestController
@Api(value ="/wx/api/comments", tags = "通用评论")
public class APICommentsController {

    @Autowired
    private CommentsService commentsService;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取资源的相关的评论列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resId", value = "评论对象 id"),
            @ApiImplicitParam(name = "resType", value = "评论对象类型：   1：feed 对象"),
            @ApiImplicitParam(name = "pageNum", value = "分页下标"),
            @ApiImplicitParam(name = "pageSize", value = "页容量")
    })
    public StatusResponse getCommentsList(HttpServletRequest req, HttpServletResponse resp,
                                          String resId, int resType,
                                          PagerQuery pagerQuery) {

         ExtCommentsSupport exto = commentsService.commentHostForResIdAndResType(resId, resType);

         return StatusResponse.success(exto);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "给资源增加评论", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "resId", value = "评论对象 id"),
            @ApiImplicitParam(name = "resType", value = "评论对象类型：   1：feed 对象"),
            @ApiImplicitParam(name = "content", value = "内容"),
            @ApiImplicitParam(name = "refCommentId", value = "回复某条评论的 id")
    })
    public StatusResponse postComment(HttpServletRequest req, HttpServletResponse resp,
                                      String resId, int resType,
                                      String content, String refCommentId) {


        WxUser user = (WxUser) req.getSession().getAttribute("loginUser");

        if(StringUtils.isEmpty(user.getParentId()) || StringUtils.isEmpty(user.getDefaultBabyId()) || StringUtils.isEmpty(user.getParentMobile())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST,"当前账号下没有宝宝信息");
        }

        String userId = user.getParentId();

        Comment comment = commentsService.addCommentForResIdAndResType(userId, user.getDefaultBabyId(), resId, resType, content, refCommentId);

        return StatusResponse.success(comment);
    }


}
